﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using CommunicationDataContract;


namespace CommunicationServiceApplication
{
    [RunInstaller(true)]
    public partial class WindowsServiceInstaller : System.Configuration.Install.Installer
    {
        public WindowsServiceInstaller()
        {
            InitializeComponent();

            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            //if you install as local service then you must run as admistrator to communicate with apps launched by user.
            processInstaller.Account = ServiceAccount.LocalService;
            serviceInstaller.StartType = ServiceStartMode.Automatic;

            serviceInstaller.ServiceName = "Communication Service Application";

            Installers.Add(serviceInstaller);
            Installers.Add(processInstaller);

            //Start Service
            this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall); 
        }

        void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            ServiceController sc = new ServiceController(EndpointSettings.WindowsServiceName);
            sc.Start();
        } 

        private ServiceInstaller serviceInstaller;
        private ServiceProcessInstaller processInstaller;

    }
}
